package com.reggie.controller;

import com.reggie.dto.DishDto;
import com.reggie.pojo.Category;
import com.reggie.pojo.Dish;
import com.reggie.pojo.DishFlavor;
import com.reggie.pojo.Employee;
import com.reggie.service.CategoryService;
import com.reggie.service.DishFlavorService;
import com.reggie.service.DishService;
import com.reggie.service.SetmealDishService;
import com.reggie.util.PageUtil;
import com.reggie.util.ResultUtil;
import com.reggie.util.SnowflakeIdWorkerUtil;
import com.reggie.util.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//菜品请求映射
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    SetmealDishService setmealDishService;

    //分页查询多条菜品信息
    @GetMapping("/page")
    public ResultUtil<PageUtil<Dish>> page(@RequestParam(value = "page", defaultValue = "0") int page,
                                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                           @RequestParam(value = "name", required = false) String name) {
        System.out.println(name);
        PageUtil<Dish> dishPage = dishService.getEmployeeList(page, pageSize, name);
        if (dishPage == null) return ResultUtil.error("未查询到菜品！");
        return ResultUtil.success(dishPage);
    }

    //删除菜品信息
    @DeleteMapping
    public ResultUtil<Null> delete(String ids){
        Map<String, Object> map = ThreadLocalUtil.get();
        Boolean isSuccess = true;
        for (String i:ids.split(",")){
            Long  id = Long.valueOf(i);
            if(dishService.delete(id, Long.valueOf((String) map.get("id")))) {
                isSuccess = true;
                continue;
            }
            isSuccess = false;
        }
        if (isSuccess) return ResultUtil.success(null);
        return ResultUtil.error("删除菜品失败！");
    }

    //添加菜品信息
    @PostMapping
    public ResultUtil<Null> add(@RequestBody DishDto dishDto){
        Map<String, Object> map = ThreadLocalUtil.get();
        dishDto.setCreateUser(Long.valueOf((String) map.get("id")));
        dishDto.setUpdateUser(Long.valueOf((String) map.get("id")));
        Long id = new SnowflakeIdWorkerUtil(0, 0).nextId();
        dishDto.setId(id);
        dishDto.setSort(0);
        Boolean isSuccess = true;
        if (dishDto.getFlavors()!=null){
            for(DishFlavor dishFlavor:dishDto.getFlavors()){
                Long flavorId = new SnowflakeIdWorkerUtil(0, 0).nextId();
                dishFlavor.setId(flavorId);
                dishFlavor.setDishId(dishDto.getId());
                dishFlavor.setCreateUser(Long.valueOf((String) map.get("id")));
                dishFlavor.setUpdateUser(Long.valueOf((String) map.get("id")));
                if (dishFlavorService.add(dishFlavor)) continue;
                isSuccess=false;
            }
        }
        if (dishService.add(dishDto)&&isSuccess) return ResultUtil.success(null);
        return ResultUtil.error("添加菜品失败！");
    }

    //编辑菜品信息
    @PutMapping
    public ResultUtil<Null> edit(@RequestBody Dish dish){
        Map<String, Object> map = ThreadLocalUtil.get();
        dish.setUpdateUser(Long.valueOf((String) map.get("id")));
        if (dishService.edit(dish)) return ResultUtil.success(null);
        return ResultUtil.error("编辑菜品失败！");
    }

    //根据id查询单条菜品信息
    @GetMapping("/{id}")
    public ResultUtil<DishDto> get(@PathVariable Long id){
        DishDto dishDto = dishService.find(id);
        List<DishFlavor> dishFlavorList = dishFlavorService.findByDishId(id);
        dishDto.setFlavors(dishFlavorList);
        if (dishDto == null) return ResultUtil.error("查询菜品信息失败！");
        System.out.println(dishDto);
        return ResultUtil.success(dishDto);
    }

    //根据状态和id修改菜品状态
    @PostMapping("/status/{status}")
    public ResultUtil<Null> status(@PathVariable("status") Integer status, String ids){
        Map<String, Object> map = ThreadLocalUtil.get();
        Boolean isSuccess = true;
        for (String i:ids.split(",")){
            Long  id = Long.valueOf(i);
            if(dishService.editStatus(id, status, Long.valueOf((String) map.get("id")))) continue;
            isSuccess = false;
        }
        if (isSuccess) return ResultUtil.success(null);
        return ResultUtil.error("修改菜品状态失败！");
    }

    //根据菜品分类或名称获取菜品信息
    @GetMapping("/list")
    public ResultUtil<List<DishDto>> list(Dish dish){
        List<DishDto> dishDtos = dishService.findByCategoryOrName(dish.getCategoryId(), dish.getName(), dish.getStatus());
        if (dishDtos==null) return ResultUtil.error("查询菜品信息失败！");
        for (DishDto dishDto:dishDtos){
            Category category = categoryService.getCategoryById(dishDto.getCategoryId());
            if (category!=null) dishDto.setCategortName(category.getName());
        }
        return ResultUtil.success(dishDtos);
    }
}
